{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Substrings of Size Three with Distinct Characters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting #sliding-window"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数 #滑动窗口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countGoodSubstrings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #长度为三且各字符不同的子字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>如果一个字符串不含有任何重复字符，我们称这个字符串为 <strong>好</strong> 字符串。</p>\n",
    "\n",
    "<p>给你一个字符串 <code>s</code> ，请你返回 <code>s</code> 中长度为 <strong>3</strong> 的 <strong>好子字符串</strong> 的数量。</p>\n",
    "\n",
    "<p>注意，如果相同的好子字符串出现多次，每一次都应该被记入答案之中。</p>\n",
    "\n",
    "<p><strong>子字符串</strong> 是一个字符串中连续的字符序列。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"xyzzaz\"\n",
    "<b>输出：</b>1\n",
    "<b>解释：</b>总共有 4 个长度为 3 的子字符串：\"xyz\"，\"yzz\"，\"zza\" 和 \"zaz\" 。\n",
    "唯一的长度为 3 的好子字符串是 \"xyz\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"aababcabc\"\n",
    "<b>输出：</b>4\n",
    "<b>解释：</b>总共有 7 个长度为 3 的子字符串：\"aab\"，\"aba\"，\"bab\"，\"abc\"，\"bca\"，\"cab\" 和 \"abc\" 。\n",
    "好子字符串包括 \"abc\"，\"bca\"，\"cab\" 和 \"abc\" 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= s.length <= 100</code></li>\n",
    "\t<li><code>s</code>​​​​​​ 只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [substrings-of-size-three-with-distinct-characters](https://leetcode.cn/problems/substrings-of-size-three-with-distinct-characters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [substrings-of-size-three-with-distinct-characters](https://leetcode.cn/problems/substrings-of-size-three-with-distinct-characters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"xyzzaz\"', '\"aababcabc\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        return sum(len(set(s[i:i+3]))== 3 for i in range(0,len(s)-2) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        # 暴力求解\n",
    "        res = 0\n",
    "        for i in range(len(s)-2):\n",
    "            sub_s = s[i:i+3]\n",
    "            # 判断sub_s是否为好串\n",
    "            if len(set(sub_s)) == 3:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        s =list(s)\n",
    "        if len(s) ==1:\n",
    "            return 0\n",
    "        total = s[:3]\n",
    "        cnt = 0 \n",
    "        if len(total) == len(set(total)):\n",
    "            cnt=1\n",
    "        for i in range(3,len(s)):\n",
    "            total.remove(s[i-3])\n",
    "            total.append(s[i])\n",
    "            if len(total) == len(set(total)):\n",
    "                cnt+=1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        lenth = len(s)\n",
    "        left = 0\n",
    "        cnt = 0\n",
    "        while left <= lenth - 3:\n",
    "            if len(set(s[left:left+3])) == 3:\n",
    "                cnt += 1\n",
    "            left += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res = 0 \n",
    "        for i in range(len(s)-2):\n",
    "            if len(set(s[i:i+3]))==3:\n",
    "                res+=1\n",
    "        return res        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        count = 0\n",
    "        n = len(s)\n",
    "        for i in range(n-2):\n",
    "            hax = set()\n",
    "            count += (s[i] != s[i+1]) and (s[i] != s[i+2]) and(s[i+1] != s[i+2])\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        n = 0\n",
    "        z = 0\n",
    "        while n <= len(s)-3:\n",
    "            if s[n] != s[n+1] and s[n+1] != s[n+2] and s[n] != s[n+2]:\n",
    "                z += 1\n",
    "            n += 1\n",
    "        return z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        #求长度为3的不重复字符滑动窗口个数\n",
    "        #用哈希集合\n",
    "        #贪心：如果ch在哈希集合中，窗口直接右移\n",
    "        h = Counter(s[:3])\n",
    "        ans = 0\n",
    "        #first window\n",
    "        if len(h) == 3:\n",
    "            ans += 1 \n",
    "        for i, ch in enumerate(s[3:], 3):\n",
    "            h[ch] += 1\n",
    "            leftch = s[i-3]\n",
    "            h[leftch] -= 1 \n",
    "            if h[leftch] == 0:\n",
    "                del h[leftch]\n",
    "            if len(h) == 3:\n",
    "                ans += 1\n",
    "        return ans \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        return [(1 if len(set(s[i:i+3])) == 3 else 0) for i in range(len(s) - 2)].count(1) if len(s) > 2 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        return sum(1 if len(set(s[j: j + 3])) == 3 else 0 for j in range(0, len(s) - 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        for i in range(n - 2):\n",
    "            if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] != s[i+2]:\n",
    "                res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        count = 0\n",
    "        for i in range(n-2):\n",
    "            if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] != s[i+2]:\n",
    "                count += 1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        num = 0\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] != s[i+2]:\n",
    "                num+=1\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "\n",
    "        ans=[]\n",
    "        n=len(s)\n",
    "        st = set()\n",
    "        res=0\n",
    "        for i in range(n-2):\n",
    "            if s[i]!=s[i+1] and s[i+1]!=s[i+2] and s[i]!=s[i+2]:\n",
    "                ans.append(s[i:i+2])\n",
    "                res+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        ret = 0\n",
    "        for i in range(len(s) - 2):\n",
    "            if len(set(s[i:i+3])) == 3:\n",
    "                ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        Show = [0]* 26\n",
    "        Len = len(s)\n",
    "        if Len < 3:\n",
    "            return 0\n",
    "        BadFlag = False\n",
    "        out = 0\n",
    "        for i in range(3):\n",
    "            temp = ord(s[i]) - ord('a')\n",
    "            Show[temp] += 1\n",
    "            if Show[temp] >= 2:\n",
    "                BadFlag = True\n",
    "        if not(BadFlag):\n",
    "            out += 1\n",
    "        for i in range(3,Len):\n",
    "            lasttemp =ord(s[i-3]) - ord('a')\n",
    "            temp = ord(s[i]) - ord('a')\n",
    "            Show[lasttemp] -= 1\n",
    "            if Show[lasttemp] == 1:\n",
    "                BadFlag = False\n",
    "            Show[temp] += 1\n",
    "            if Show[temp] >= 2:\n",
    "                BadFlag = True\n",
    "            if not (BadFlag):\n",
    "                out += 1\n",
    "        return out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        lenth = len(s)\n",
    "        left = 0\n",
    "        cnt = 0\n",
    "        while left <= lenth - 3:\n",
    "            ch_list = []\n",
    "            for i in s[left: left + 3]:\n",
    "                if i not in ch_list:\n",
    "                    ch_list.append(i)\n",
    "                else:\n",
    "                    continue\n",
    "                if len(ch_list) == 3:\n",
    "                    cnt += 1\n",
    "            left += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        cnt = 0 \n",
    "        l = len(s)\n",
    "        for i in range(1, l - 1):\n",
    "            if s[i] != s[i + 1] and s[i] != s[i - 1] and s[i - 1] != s[i + 1]:\n",
    "                cnt += 1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        if n<3:\n",
    "            return 0\n",
    "        subStr = [s[0],s[1],s[2]]\n",
    "        cnt = 1 if len(set(subStr))==3 else 0\n",
    "        for i in range(3,n):\n",
    "            subStr.pop(0)\n",
    "            subStr.append(s[i])\n",
    "            if len(set(subStr))==3:\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res=[]        \n",
    "        for i in  range(len(s)-2):\n",
    "            if len(s[i:i+3])==len(set(s[i:i+3])):\n",
    "                res.append(s[i:i+3])                \n",
    "        return len(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        _set=set()\n",
    "        res=l=0\n",
    "        for r in range(len(s)):\n",
    "            while s[r] in _set or r-l+1>3:\n",
    "                _set.remove(s[l])\n",
    "                l+=1\n",
    "            _set.add(s[r])\n",
    "            if r-l+1==3: res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        lenth = len(s)\n",
    "        left = 0\n",
    "        cnt = 0\n",
    "        while left <= lenth - 3:\n",
    "            ch_list = []\n",
    "            for i in s[left: left + 3]:\n",
    "                if i not in ch_list:\n",
    "                    ch_list.append(i)\n",
    "                else:\n",
    "                    continue\n",
    "                if len(ch_list) == 3:\n",
    "                    cnt += 1\n",
    "            left += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        if len(s)<3:\n",
    "            return 0\n",
    "        ans=len(s)-2\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i]!=s[i+1] and s[i+2]!=s[i+1] and s[i]!=s[i+2] :\n",
    "                pass\n",
    "            else:\n",
    "                ans-=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        return sum(len(set(s[i-2:i+1])) == 3  for i in range(2,len(s))) if len(s) >= 3 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        return sum(len(set(s[i:i+3])) == 3 for i in range(len(s)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        count=0\n",
    "        for i in range(len(s)):\n",
    "            if len(set(s[i:3+i]))==3:\n",
    "                count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        count = 0\n",
    "        n = len(s)\n",
    "        for i in range(n-2):\n",
    "            count += (s[i] != s[i+1]) and (s[i] != s[i+2]) and(s[i+1] != s[i+2])\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        for i in range(n-2):\n",
    "            if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] != s[i+2]:\n",
    "                res +=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        cnt = 0 \n",
    "        l = len(s)\n",
    "        for i in range(1, l - 1):\n",
    "            if s[i] != s[i + 1] and s[i] != s[i - 1] and s[i - 1] != s[i + 1]:\n",
    "                cnt += 1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        num=0\n",
    "        for i in range(0,len(s)-2):\n",
    "            if s[i]!=s[i+1] and s[i+1]!=s[i+2] and s[i+2]!=s[i]:\n",
    "                num+=1\n",
    "        return num   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        if len(s)<3:return 0\n",
    "        for i in range(len(s)-2):\n",
    "            # print(set(list(s[i:i+3])),s[i:i+3])\n",
    "            if len(s[i:i+3])==len(set(list(s[i:i+3]))):\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res=0\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i]!=s[i+1] and s[i+1]!=s[i+2] and s[i]!=s[i+2]:\n",
    "                res+=1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i <= len(s) - 3:\n",
    "            if s[i] != s[i + 1] and s[i] != s[i + 2] and s[i + 1] != s[i + 2]:\n",
    "                ans += 1\n",
    "            i += 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        cnt = 0\n",
    "        for i in range(len(s) - 2):\n",
    "            if len(list(set(s[i:i + 3]))) == 3:\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        n, count = len(s), 0\n",
    "        if n < 3:\n",
    "            return 0\n",
    "        for i in range(n - 2):\n",
    "            s1 = s[i:i + 3]\n",
    "            if s1[0] != s1[1] and s1[1] != s1[2] and s1[0]!=s1[2]:\n",
    "                count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        if len(s) < 3:\n",
    "            return 0\n",
    "        return sum(1 for i in range(len(s) - 2) if len(set(s[i:i+3])) == 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        l,r=0,3\n",
    "        count=0\n",
    "        while l<len(s):\n",
    "            if len(set(s[l:r]))==3:\n",
    "                count+=1\n",
    "            l+=1\n",
    "            r+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        lenth = len(s)\n",
    "        left = 0\n",
    "        cnt = 0\n",
    "        while left <= lenth - 3:\n",
    "            ch_list = []\n",
    "            for i in s[left: left + 3]:\n",
    "                if i not in ch_list:\n",
    "                    ch_list.append(i)\n",
    "                else:\n",
    "                    continue\n",
    "                if len(ch_list) == 3:\n",
    "                    cnt += 1\n",
    "            left += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        count = 0\n",
    "        l = len(s)\n",
    "        for i in range(l-2):\n",
    "            if all(value == 1 for value in Counter(s[i:i+3]).values()) :\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        ans=0\n",
    "        def isgood(x):\n",
    "            return len(x)==len(set(x))\n",
    "        if len(s)<3:\n",
    "            return ans\n",
    "        for i in range(len(s)-2):\n",
    "            if isgood(s[i:i+3]):\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        return sum(len(set(s[i:i+3])) == 3 for i in range(len(s)-2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        le = len(s)\n",
    "        num = 0\n",
    "        for i in range(2, le):\n",
    "            if s[i] != s[i - 1] and s[i] != s[i - 2] and s[i - 1] != s[i - 2]:\n",
    "                num += 1\n",
    "            else:\n",
    "                i += 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        start = 0\n",
    "        count = 0\n",
    "        while start + 2 < len(s):\n",
    "            if s[start] != s[start + 1] and s[start] != s[start + 2] and s[start + 1] != s[start + 2]:\n",
    "                count += 1\n",
    "            start += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        count = 0\n",
    "        n = len(s)\n",
    "        for i in range(n-2):\n",
    "            count += (s[i] != s[i+1]) and (s[i] != s[i+2]) and(s[i+1] != s[i+2])\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        result = 0\n",
    "        for i in range(n-2):\n",
    "            if s[i] != s[i+1] and s[i+1] != s[i+2] and s[i] != s[i+2]:\n",
    "                result += 1\n",
    "        return result \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        '''\n",
    "            限定长度为 3，所以原始字符串也就 3 个 3 个的找就可以        \n",
    "        '''\n",
    "        ans=[]\n",
    "        n=len(s)\n",
    "        st = set()\n",
    "        res=0\n",
    "        for i in range(n-2):\n",
    "            if s[i]!=s[i+1] and s[i+1]!=s[i+2] and s[i]!=s[i+2]:\n",
    "                ans.append(s[i:i+2])\n",
    "                res+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(s) - 2):\n",
    "            if len(set(s[i: i + 3])) == 3:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        if n < 3:\n",
    "            return 0\n",
    "        result = 0\n",
    "        for i in range(n - 3 + 1):\n",
    "            if len(set(s[i: i+3])) == 3:\n",
    "                result += 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        if len(s) < 3:\n",
    "            return 0\n",
    "        for i in range(len(s) - 2):\n",
    "            a, b, c = s[i], s[i + 1], s[i + 2]\n",
    "            if len({a, b, c}) == 3:\n",
    "                # if a != b and b != c and a != c:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        return sum(len(set(s[i:i+3])) == 3 for i in range(len(s)-2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        lenth = len(s)\n",
    "        left = 0\n",
    "        cnt = 0\n",
    "        while left <= lenth - 3:\n",
    "            ch_list = []\n",
    "            for i in s[left: left + 3]:\n",
    "                if i not in ch_list:\n",
    "                    ch_list.append(i)\n",
    "                else:\n",
    "                    continue\n",
    "                if len(ch_list) == 3:\n",
    "                    cnt += 1\n",
    "            left += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res=0\n",
    "        n=len(s)\n",
    "        for i in range(n-2):\n",
    "            if len(s[i:i+3])==len(list(set(s[i:i+3]))):\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        for i in range(n - 2):\n",
    "            if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] != s[i+2]:\n",
    "                res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        a = 0\n",
    "        if n < 3:\n",
    "            return 0\n",
    "        \n",
    "        for i in range(n-2):\n",
    "            times = Counter()\n",
    "            for j in s[i:i+3]:\n",
    "                if times[j]:\n",
    "                    break\n",
    "                times[j] += 1\n",
    "            else:\n",
    "                a += 1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        counts = {}\n",
    "        n = len(s)\n",
    "        if n<3:\n",
    "            return 0\n",
    "        for i in range(3):\n",
    "            if s[i] in counts:\n",
    "                counts[s[i]] +=1\n",
    "            else:\n",
    "                counts[s[i]] = 1\n",
    "        res = 0\n",
    "        ans = 1\n",
    "        for key in counts:\n",
    "            ans \n",
    "            ans *= counts[key]\n",
    "        if ans == 1:\n",
    "            res += 1\n",
    "        for i in range(3,n):\n",
    "            if s[i] in counts:\n",
    "                counts[s[i]] +=1\n",
    "            else:\n",
    "                counts[s[i]] = 1\n",
    "            counts[s[i-3]] -=1\n",
    "            ans = 1\n",
    "            for j in range(i-2,i+1):\n",
    "                ans *= counts[s[j]]\n",
    "            if ans == 1:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        if len(s) <= 2:\n",
    "            return 0\n",
    "        res = 0\n",
    "        for i in range(len(s) - 2):\n",
    "            if s[i] != s[i + 1] and s[i] != s[i + 2] and s[i + 1] != s[i + 2]:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        num = 0\n",
    "        for i in range(len(s)):\n",
    "            temp = s[i:i+3]\n",
    "            if len(set(temp))==3:\n",
    "                num+=1\n",
    "        return (num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        #固定滑窗\n",
    "        n = len(s)\n",
    "        res = 0\n",
    "        for i in range(2,n):\n",
    "            if s[i]!=s[i-1] and s[i]!=s[i-2] and s[i-1]!=s[i-2]:\n",
    "                res+=1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        # 暴力法\n",
    "        def isgoodstr(s: str) -> bool:\n",
    "            if s[0] != s[1] and s[0] != s[2] and s[1] != s[2]:\n",
    "                return True\n",
    "            return False\n",
    "        \n",
    "        if len(s) < 3:\n",
    "            return 0\n",
    "        \n",
    "        i = 0\n",
    "        res = 0\n",
    "        while i + 3 <= len(s):\n",
    "            tmp = s[i:i+3]\n",
    "            if isgoodstr(tmp):\n",
    "                res += 1\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        result=0\n",
    "        for i in range(len(s)-2):\n",
    "            if len(set(s[i:i+3]))==3:\n",
    "                result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(s) - 2):\n",
    "            if len(set(s[i: i + 3])) == 3:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        from collections import Counter\n",
    "\n",
    "        cnt = Counter(s[:3])\n",
    "\n",
    "        res = 1 if max(cnt.values())==1 and len(cnt) == 3 else 0\n",
    "        \n",
    "        for right, x in enumerate(s[3:], 3):\n",
    "            cnt[x] +=1\n",
    "            cnt[s[right-3]] -= 1\n",
    "            if max(cnt.values()) == 1:\n",
    "                res +=1\n",
    "        return res\n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "    \n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(s) - 2):\n",
    "            temp = s[i:i+3]\n",
    "            if len(temp) == len(set(temp)):\n",
    "                count += 1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        from collections import Counter\n",
    "\n",
    "        cnt = Counter(s[:3])\n",
    "\n",
    "        res = 1 if max(cnt.values())==1 and len(cnt) == 3 else 0   #如果s的长度<3，res = 0\n",
    "        \n",
    "        for right, x in enumerate(s[3:], 3):\n",
    "            cnt[x] +=1\n",
    "            cnt[s[right-3]] -= 1\n",
    "            if max(cnt.values()) == 1:\n",
    "                res +=1\n",
    "        return res\n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "    \n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(s)-2):\n",
    "            if len(set(s[i:i+3])) == 3:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        res = 0\n",
    "        if len(s) < 3:\n",
    "            return res\n",
    "\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len - 2):\n",
    "            if len(set(list(s[i:i + 3]))) == 3:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        temp = []\n",
    "        co = 0\n",
    "        for i in range(len(s)-2):\n",
    "            temp.append(s[i:i+3])\n",
    "        for tmp in temp:\n",
    "            if len(set(tmp)) == len(tmp):\n",
    "                co += 1\n",
    "        return co"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        count = 0\n",
    "        n = len(s)\n",
    "        for i in range(n-2):\n",
    "            count += 3 == len(set(s[i:i+3]))\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        if len(s) < 3:\n",
    "            return 0 \n",
    "    \n",
    "        cnt = [0] * 26\n",
    "        repeat = False\n",
    "        for i in range (0, 3):\n",
    "            idx = ord(s[i]) - ord('a')\n",
    "            if cnt[idx] > 0:\n",
    "                repeat = True\n",
    "            cnt[idx] += 1\n",
    "\n",
    "        res = 0\n",
    "        if False == repeat:\n",
    "            res += 1\n",
    "\n",
    "        n = len(s)\n",
    "        for i in range (1, n - 2):\n",
    "            idx0 = ord(s[i - 1]) - ord('a')\n",
    "            idx1 = ord(s[i]) - ord('a')\n",
    "            idx2 = ord(s[i + 1]) - ord('a')\n",
    "            idx3 = ord(s[i + 2]) - ord('a')\n",
    "            cnt[idx0] -= 1\n",
    "            cnt[idx3] += 1\n",
    "            if 1 == cnt[idx1] and 1 == cnt[idx2] and 1 == cnt[idx3]:\n",
    "                res += 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countGoodSubstrings(self, s: str) -> int:\n",
    "        if len(s) < 3:\n",
    "            return 0\n",
    "        c = 0\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] != s[i+2]:\n",
    "                # print(s[i:i+3])\n",
    "                c += 1\n",
    "        return c"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
